// class header for .sgi image
#pragma once
#include <string>
#include <iostream>
#include <vector>
#include "Image.h"
#include "SGImageScanline.h"

struct Pixel {
	unsigned char r;
	unsigned char g;
	unsigned char b;
	unsigned char a;
};

class SGImage
{
public:
	SGImage( const std::string filename );
	~SGImage( void );
// header info
private:
	unsigned short h_magic; // magic number, always 474
	unsigned char h_storage; // storage format
	unsigned char h_bpc; // pixels per channel
	unsigned short h_dimension; // number of dimensions
	unsigned short h_xsize; // x size in pixels
	unsigned short h_ysize; // y size in pixels
	unsigned short h_zsize; // number of channels
	long h_pixmin; // minimum pixel value, usually 0
	long h_pixmax; // maximum pixel value, usually 255
	char h_dummy; // ignored
	char h_imagename[80]; // name of image
	long h_colormap; // pixel data format
	unsigned char h_dummy2[404]; // ignored
	unsigned char *iptr;
//offset tables (if RLE)
private:
	unsigned long *ot_starttab;
	unsigned long *ot_lengthtab;
private:
	FILE *imageFile;
	std::string m_filename;
	std::vector< SGImageScanline* > v_lines;
	std::vector< SGImageScanline* > v_dec_lines;
	Pixel *pixels;
public:
	void loadSGImage( Image *p_image );
private:
	void readHeader( void );
	void readTables( void );
	unsigned short getShort( void );
	long getLong( void );
	unsigned char getByte( void );
	void readlongtab( int n, unsigned long *tab );
	int RLElength(int r, int c);
	int RLEoffset(int r, int c);
	int readScanline( int r, int c, int which_color );
	SGImageScanline* findEqualScanline( const int start, const int length );
	void decompressScanline( const int y, const int channel );
	void readByteTab( unsigned int n, unsigned char *tab );
};


